const nodemailer = require("nodemailer");
const MAIL_USER = '272935547@qq.com'
const MAIL_PASS = 'pockmabsyxfhbgbc'; //授权码
const mailConf = {
    host: "smtp.qq.com",
    secureConnection: true,
    port: 465,
    auth: {
        user: MAIL_USER,
        pass: MAIL_PASS
    },
    // auth: {
    //   user: "git@lucia.vip",
    //   pass: "Zeus.123ok"
    // }
};

module.exports = function(mailOpts = { subject: "", content: "", attachment: "" }, cb = null) {
    mailOpts.content = typeof mailOpts.content == 'object' ? JSON.stringify(mailOpts.content, '\t', 2) : mailOpts.content;
    var mailOptions = {
        from: `NODE-API ${MAIL_USER}`,
        to: "tellnobody@163.com", //多邮箱用,分割
        subject: mailOpts.subject,
        text: mailOpts.content, // plain text body
        html: "<body>" + mailOpts.content + "</body>",
        // attachments: [
        //   {
        //     filename: mailOpts.attachment,
        //     path: "../logs/" + mailOpts.attachment
        //   }
        // ]
    };

    var transport = nodemailer.createTransport(mailConf);

    // send mail with defined transport object
    transport.sendMail(mailOptions, (err, res) => {
        if (err) {
            console.log("Error occurred: ", err.message);
            // return process.exit(1);
        } else {
            console.log("Message [%s] sent: %s", mailOpts.subject, res.messageId);
            transport.close();
            cb && cb();
        }
    });
};